package com.demo.spark.rdd;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;

public class SparkRddJoin2 {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setAppName("SparkRDD").setMaster("local");

        JavaSparkContext sc = new JavaSparkContext(conf);

        List<Integer> data = Arrays.asList(1,2,3,4,5);
        JavaRDD<Integer> rdd = sc.parallelize(data);
        //FirstRDD
        JavaPairRDD<Integer, Integer> firstRDD = rdd.mapToPair(new PairFunction<Integer,Integer,Integer>(){
            @Override
            public Tuple2<Integer, Integer> call(Integer m) throws Exception {
                return new Tuple2<>(m, m * m);
            }
        });

        JavaPairRDD<Integer, String> secondRdd = rdd.mapToPair(new PairFunction<Integer, Integer, String>() {
            @Override
            public Tuple2<Integer, String> call(Integer integer) throws Exception {
                return new Tuple2<>(integer, String.valueOf((char) (integer + 64)));
            }
        });


        JavaPairRDD<Integer, Tuple2<Integer, String>> join = firstRDD.join(secondRdd);
        JavaRDD<String> map = join.map(new Function<Tuple2<Integer, Tuple2<Integer, String>>, String>() {
            @Override
            public String call(Tuple2<Integer, Tuple2<Integer, String>> v1) throws Exception {
                Integer key = v1._1;
                Tuple2<Integer, String> integerStringTuple2 = v1._2;
                Integer value1 = integerStringTuple2._1;
                String value2 = integerStringTuple2._2;

                return "<" + key + ", <" + value1 + "," + value2 + ">>";
            }
        });
        map.foreach(new VoidFunction<String>() {
            @Override
            public void call(String s) throws Exception {
                System.out.println(s);
            }
        });


        sc.stop();

    }
}
